package com.chen.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * <b>针对拦截</b>
 * <p>
 * 描述:<br>
 * 该拦截器需要实现Filter接口<br>
 * 目的：拦截指定页面<br>
 * 配置文件需给值<br>
 * <init-param><br>
 * 	<param-name>pages<br>
 * 	<param-value>以‘，’分隔开，经过处理将会成为数组<br>
 * @author 威 
 * <br>2018年5月22日 下午10:06:02 
 * @see
 * @since 1.0
 */
public class TargetFilter implements Filter{
	private String[] pages;
	
	@Override
	public void destroy() {
		System.out.println(this.getClass().getSimpleName()+"销毁");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain)
			throws IOException, ServletException {
		if(pages.length > 0){
			for(int i = 0; i < pages.length; i++){
				if(((HttpServletRequest) request).getServletPath().equals(pages[i])){
					System.out.println("是目标页面--》进行拦截，页面名称为:"+pages[i]);
				}
			}
		}
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println(this.getClass().getSimpleName()+"初始化");
		String param = arg0.getInitParameter("pages");
		if(param.length() > 0)
			pages = param.split(",");
	}

}
